package com.gilight.base.admin.brand;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gilight.base.base.exception.AdminServiceException;
import com.gilight.base.base.exception.ExceptionDefinition;
import com.gilight.base.base.exception.ServiceException;
import com.gilight.base.component.CacheComponent;
import com.gilight.base.component.CacheConst;
import com.gilight.base.mapper.BrandMapper;
import com.gilight.base.model.BrandDo;
import com.gilight.base.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: kbq
 * Date: 2019-07-08
 * Time: 下午9:24
 */
@Service("adminBrandService")
public class AdminBrandServiceImpl implements AdminBrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CacheComponent cacheComponent;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(String brandName, String imgUrl, Integer status, Integer orderC, String myimgUrl, String favicon, String inBuildUrl) throws ServiceException {
        Date now = new Date();
        BrandDo brandDo = new BrandDo();
        brandDo.setBrandName(brandName);
        brandDo.setImgUrl(imgUrl);
        brandDo.setStatus(status);
        brandDo.setGmtCreate(now);
        brandDo.setGmtUpdate(now);
        brandDo.setOrderC(orderC);
        brandDo.setMyimgUrl(myimgUrl);
        brandDo.setFavicon(favicon);
        brandDo.setInBuildUrl(inBuildUrl);
        if (brandMapper.insert(brandDo) > 0) {
            this.clearCache(brandDo.getId());
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.BrandISEMENT_SQL_ADD_FAILED);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long bdId) throws ServiceException {
        BrandDo brandDo = new BrandDo();
        brandDo.setId(bdId);
        brandDo.setStatus(-1); //逻辑删除
        if (brandMapper.updateById(brandDo) > 0) {
            this.clearCache(bdId);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.BrandISEMENT_SQL_ADD_FAILED);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long bdId, String brandName, String imgUrl, Integer status, Integer orderC, String myimgUrl, String favicon, String inBuildUrl) throws ServiceException {
        BrandDo brandDo = new BrandDo();
        brandDo.setId(bdId);
        brandDo.setBrandName(brandName);
        brandDo.setImgUrl(imgUrl);
        brandDo.setStatus(status);
        brandDo.setGmtUpdate(new Date());
        brandDo.setOrderC(orderC);
        brandDo.setMyimgUrl(myimgUrl);
        brandDo.setFavicon(favicon);
        brandDo.setInBuildUrl(inBuildUrl);
        if (brandMapper.updateById(brandDo) > 0) {
            this.clearCache(bdId);
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.BrandISEMENT_SQL_ADD_FAILED);
    }

    @Override
    public Page<BrandDo> list(Integer page, Integer limit, Integer status) throws ServiceException {
        QueryWrapper<BrandDo> wrapper = new QueryWrapper<BrandDo>();

        if (status != null) {
            wrapper.eq("status", status);
        }
        if(page == null){
            page = 1;
        }
        wrapper.gt("status",-1);
        wrapper.orderByAsc("order_c");
        return brandMapper.selectPage(Page.div(page, limit, BrandDo.class), wrapper);
    }

    @Override
    public BrandDo getById(Long id) {
        if(cacheComponent.hasKey(CacheConst.BRAND_ID + ":"+id)){
           return cacheComponent.getObj(CacheConst.BRAND_ID + ":"+id,BrandDo.class);
        }
        QueryWrapper<BrandDo> wrapper = new QueryWrapper<BrandDo>();
        wrapper.eq("id",id);
        BrandDo brandDo = brandMapper.selectOne(wrapper);
        cacheComponent.putObj(CacheConst.BRAND_ID + ":"+id,brandDo);
        return brandDo;
    }

    public void clearCache(Long brandId) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                cacheComponent.del(CacheConst.BRAND_ID + ":"+brandId);
            }
        });
    }
    public BrandDo getBrandBySer(String ser){
        return null;
    }
    public void addCache(Long brandId,BrandDo brandDo) {
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                cacheComponent.putObj(CacheConst.BRAND_ID + ":"+brandId,brandDo);
            }
        });
    }
}
